package tranduythanh.com;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity implements OnClickListener{

	Button btndom;
	EditText editdata;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		btndom=(Button) findViewById(R.id.btndomparser);
		btndom.setOnClickListener(this);
		editdata=(EditText) findViewById(R.id.editdata);
	}
	public void saxparser()
	{
		try {
			XmlPullParserFactory fc=XmlPullParserFactory
					.newInstance();
			XmlPullParser parser= fc.newPullParser();

			String sdcard=Environment.
					getExternalStorageDirectory().
					getAbsolutePath();
			String xmlfile=sdcard+"/employee.xml";
			FileInputStream fIn=new 
					FileInputStream(xmlfile);

			parser.setInput(fIn, "UTF-8");
			
			int eventType=-1;
			String nodeName;
			String datashow="";
			while(eventType!=XmlPullParser.END_DOCUMENT)
			{
				eventType=parser.next();
				switch(eventType)
				{
				case XmlPullParser.START_DOCUMENT:
					break;
				case XmlPullParser.END_DOCUMENT:
					break;
				case XmlPullParser.START_TAG:
					nodeName=parser.getName();
					if(nodeName.equals("employee")){
						datashow+=parser.getAttributeValue(0)+"-";
						datashow+=parser.getAttributeValue(1)+"-";
					}
					else if(nodeName.equals("name")){
						datashow+=parser.nextText()+"-";
					}
					else if(nodeName.equals("phone")){
						datashow+=parser.nextText()+"-";
					}
					break;
				case XmlPullParser.END_TAG:
					nodeName=parser.getName();
					if(nodeName.equals("employee")){
						datashow+="\n----------------\n";
					}
					break;
				}
			}
			editdata.setText(datashow);

		} catch (XmlPullParserException e) {
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if(v.getId()==R.id.btndomparser)
		{
			saxparser();
		}
	}
}
